package com.sparrow.source.common.dto.convert;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sparrow.source.common.dto.UnvPerson;
import com.sparrow.source.common.dto.UnvPersonItem;
import com.sparrow.source.persistense.IasDeviceUser;
import com.sparrow.source.persistense.IasPerson;
import com.sparrow.source.persistense.IasUser;
import com.sparrow.source.service.IIasPersonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author bryceyung
 * @since 2024/11/27
 */
@Component
@Slf4j
public class IasPersonConvert {

    @Autowired
    private IIasPersonService personService;

    public List<IasPerson> unvPersonItemConvert(UnvPerson unvPerson) {
        List<IasPerson> personList = Lists.newArrayList();
        List<UnvPersonItem> itemList = unvPerson.getPersonInfoList();
        itemList.forEach(data -> {
            IasPerson person = new IasPerson();
            person.setCreateTime(new Date());
            person.setPersonId(data.getPersonId());
            person.setName(data.getPersonName());
            if (data.getImageList().size() > 0) {
                person.setData(data.getImageList().get(0).getData());
            }
            IasPerson p = personService.lambdaQuery().eq(IasPerson::getPersonId, data.getPersonId()).one();
            if (p != null) {
                person.setId(p.getId());
            }
            personList.add(person);
        });
        return personList;
    }

    public List<IasDeviceUser> unvPersonItemConvertIasUser(Integer deviceId, List<UnvPersonItem> personItemList) {
        List<IasDeviceUser> deviceUsers = Lists.newArrayList();
        personItemList.forEach(personItem -> {
            IasDeviceUser user = new IasDeviceUser();
            if (personItem.getPersonId() < Integer.MAX_VALUE) {
                user.setUid(personItem.getPersonId());
                user.setDeviceId(deviceId);
                user.setDeleted(false);
                if (CollectionUtils.isNotEmpty(personItem.getImageList())) {
                    user.setImageData(personItem.getImageList().get(0).getData());
                }
                user.setSyncTime(new Date());
                deviceUsers.add(user);
            }
        });
        return deviceUsers;
    }

}
